///<reference path="../../../core/mengdao/api/mvc/command/BaseCommand.ts"/>
/**
 * Created by PengWei on 2018/7/26.
 */
namespace game {
    import BaseCommand = mengdao.BaseCommand;
    import ResourceEvent = RES.ResourceEvent;
    import UIPackage = fairygui.UIPackage;

    export class InitProcessCommand extends BaseCommand {
        public constructor() {
            super();
        }

        private _processList: Function[];
        private _finishCall: Function;

        public execute(data: any): void {
            this.resetNormalProcess();
            this.checkNext();
        }

        private resetNormalProcess(): void {
            this._processList = [
                this.prepareResConfig,
                this.initLayer,
                this.showLogin,
                this.prepareUI
            ]
        }


        private prepareResConfig(): void {
            let gotCfg = ()=>{
                RES.removeEventListener(ResourceEvent.CONFIG_COMPLETE, gotCfg, this);
                // FairyHelper.replaceGClass();
                RES.addEventListener(ResourceEvent.GROUP_COMPLETE, gotPreload, this);
                RES.loadGroup('preload')
            }
            let gotPreload = (evt:ResourceEvent)=>{
                if(evt.groupName!='preload') return;
                RES.removeEventListener(ResourceEvent.GROUP_COMPLETE, gotPreload, this);
                common.commonBinder.bindAll();
                UIPackage.addPackage("common")
                this.checkNext();
            }
            RES.addEventListener(ResourceEvent.CONFIG_COMPLETE, gotCfg, this);
            RES.loadConfig('resource/resource.json', 'resource/');
        }

        private prepareFirstUI(): void {
            let gotCfg = (evt:ResourceEvent)=>{
                if(evt.groupName == 'first'){
                    RES.removeEventListener(ResourceEvent.GROUP_COMPLETE, gotCfg, this);
                    RES.removeEventListener(ResourceEvent.GROUP_PROGRESS, gotProcess,this);
                    this.checkNext();
                }
            }

            let gotProcess = (evt:ResourceEvent)=>{
                window["game_setLoadingProcess"](Math.floor(evt.itemsLoaded / evt.itemsTotal * 100));
            }

            RES.addEventListener(ResourceEvent.GROUP_COMPLETE, gotCfg, this);
            RES.addEventListener(ResourceEvent.GROUP_PROGRESS, gotProcess,this);
            RES.loadGroup('first');
            window["game_setLoadingProcess"](0);
            window["game_setLoadingTxt"]("美术正在画素材...");
        }

        private prepareCityMusic(): void {
            RES.getResAsync("city_mp3",()=>
            {
                if(timeOutId)clearTimeout(timeOutId);
                window["game_setLoadingProcess"](100);
                window["game_setLoadingTxt"]("准备好了!");
                this.checkNext();
            },this);
            let timeOutId = setTimeout(()=>
            {
                window["game_setLoadingProcess"](90);
            },100);
            window["game_setLoadingProcess"](0);
            window["game_setLoadingTxt"]("音效师正在配乐...");
        }

        private initStaticData(): void {
            StaticDataUtils.prepareData(()=>
            {
                if(timeOutId)clearTimeout(timeOutId);
                window["game_setLoadingProcess"](100);
                window["game_setLoadingTxt"]("准备好了!");
                this.checkNext();
            }, this);

            let timeOutId = setTimeout(()=>
            {
                window["game_setLoadingProcess"](90);
            },100);
            window["game_setLoadingProcess"](0);
            window["game_setLoadingTxt"]("设计师调整数据...");
        }

        private initLayer(): void {
            this.dispatchEventWith(GameEvent.INIT_LAYER);
            this.checkNext();
        }

        private showLogin(): void {
            this.dispatchEventWith(GameEvent.EXECUTE_LOGIN);
            this.checkNext();
        }

        private prepareUI(): void {
            //准备UI内容
        }

        private checkNext(): void {
            if (this._processList.length > 0) {
                this._processList.shift().apply(this);
            } else if (this._finishCall) {
                this._finishCall.apply(this);
            }
        }

        public dispose(): void {
            this._processList = null;
            this._finishCall = null;
        }
    }
}